﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Manul.Genetics
{
    public class PropertyValue : INotifyPropertyChanged
    {
        public PropertyValue()
        { }
        public PropertyValue(double doubleValue)
        {
            DoubleValue = doubleValue;
            StringValue = doubleValue.ToString();
            valueType = PropertyValueType.Number;
        }
        public PropertyValue(string stringValue)
        {
            StringValue = doubleValue.ToString();
        }
        private double doubleValue = 0;
        private string stringValue = String.Empty;

        private readonly char[] CommandValues = { '+', '-', '*', '/' };

        private PropertyValueType valueType = PropertyValueType.String;
        public PropertyValueType ValueType
        {
            get
            {
                return valueType;
            }
        }
        private ModificationType modification = ModificationType.Equal;
        public ModificationType Modification
        {
            get
            {
                return modification;
            }
        }

        private string key = String.Empty;
        public string Key
        {
            get
            {
                return key;
            }
            set
            {
                key = value;
                this.NotifyPropertyChanged("Key");
            }

        }

        public string StringValue
        {
            get
            {
                return stringValue;
            }
            set
            {
                stringValue = value;
                double tempDouble = 0;
                StringBuilder valueToParse = new StringBuilder(value);
                if (valueToParse.Length > 0)
                {
                    if (valueToParse.Length > 1)
                    {
                        char firstSymbol = valueToParse[0];
                        if (CommandValues.Contains(firstSymbol))
                        {
                            switch (firstSymbol)
                            {
                                case '+':
                                    modification = ModificationType.Plus;
                                    break;
                                case '-':
                                    modification = ModificationType.Minus;
                                    break;
                                case '*':
                                    modification = ModificationType.Multiply;
                                    break;
                                case '/':
                                    modification = ModificationType.Divide;
                                    break;
                                default:
                                    modification = ModificationType.Equal;
                                    break;
                            }
                            valueToParse.Remove(0, 1);
                        }
                    }
                    if (Double.TryParse(valueToParse.ToString(), out tempDouble))
                    {
                        doubleValue = tempDouble;
                        valueType = PropertyValueType.Number;
                    }
                    else
                    {
                        doubleValue = 0;
                        valueType = PropertyValueType.String;
                    }
                }
                this.NotifyPropertyChanged("StringValue");
            }
        }
        public double DoubleValue
        {
            get
            {
                return doubleValue;
            }
            private set
            {
                doubleValue = value;
                stringValue = value.ToString();
                valueType = PropertyValueType.Number;
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
        #endregion
    }
    public enum PropertyValueType
    {
        String,
        Number
    }

    public enum ModificationType
    {
        Equal,
        Plus,
        Minus,
        Multiply,
        Divide
    }
}
